<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use session;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class indentController extends Controller
{
    //用户订单页面
    public function indent(Request $request)
    {
      $dd = ($request -> all());
      //dd($dd);
      // 下订单
      $uid = $dd['uid'];
      $res = DB::table('goods') -> where('id',$request -> gid) ->first();
      
      $data = DB::table('users') -> where('id',$request -> uid) ->first();
      
      $req = $request ->except('_token');
      date_default_timezone_set('PRC');
      $time = date("Y-m-d H:i:s");
      //执行插入数据库
      $query = DB::table('indent') -> insert(['gid'=> $dd['gid'],'uid'=>$dd['uid'],'tid'=> $dd['tid'],'site'=> $dd['site'],'time'=> $time]);
      //dd($query);
      if($query)
      {
      	return redirect("/home/indent/menu/$uid");
      }else{
      	return back() ;
      }
      
    }
    public function menu($id)
    {
    	$data = DB::table('indent as i')
           -> select('i.*','u.*','g.*','i.id as iid')
           -> leftjoin('users as u','u.id','=','i.uid')
           -> leftjoin('goods as g','g.id','=','i.gid')
           -> where('u.id',$id)
           -> get();
        //dd($data);
        return view('home.indent.menu',['data' => $data]);
    	
    }
    //付款操作*****************
    public function pay($id)
    {
      date_default_timezone_set('PRC');
      $time = date("Y-m-d H:i:s");
      $data = array('pay'=>'1','time' => $time);
      $res = DB::table('indent')
          -> where('id',$id)
          -> update($data);
       $uid = session('master') -> id;  
      if($res)
      {
        return redirect("/home/indent/menu/$uid");
      }
        return back();
    }
    //退款操作**********************
    public function unpay($id)
    {

      date_default_timezone_set('PRC');
      $time = date("Y-m-d H:i:s");
      $data = array('pay'=>'0','time' => '');
      $res = DB::table('indent')
          -> where('id',$id)
          -> where('pass','0')
          -> update($data);
       $uid = session('master') -> id;  
      if($res)
      {
        return redirect("/home/indent/menu/$uid");
      }
        return back();
    }


}
